package com.ruoyi.mall.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.mall.domain.UserAddress;
import com.ruoyi.mall.service.UserAddressService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户收货地址
 *
 * @author JL
 * @date 2019-09-11 14:28:59
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/useraddress")
public class UserAddressController extends BaseController {

    private final UserAddressService userAddressService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param userAddress 用户收货地址
    * @return
    */
    @GetMapping("/page")
    @SaCheckPermission("mall:useraddress:index")
    public R<Object> getUserAddressPage(Page<UserAddress> page, UserAddress userAddress) {
        return R.ok(userAddressService.page(page,Wrappers.query(userAddress)));
    }

    /**
    * 通过id查询用户收货地址
    * @param id
    * @return R
    */
    @GetMapping("/{id}")
    @SaCheckPermission("mall:useraddress:get")
    public R<UserAddress> getById(@PathVariable("id") String id){
        return R.ok(userAddressService.getById(id));
    }

    /**
    * 新增用户收货地址
    * @param userAddress 用户收货地址
    * @return R
    */
    @PostMapping
    @SaCheckPermission("mall:useraddress:add")
    public R<Void> save(@RequestBody UserAddress userAddress){
        return toAjax(userAddressService.save(userAddress));
    }

    /**
    * 修改用户收货地址
    * @param userAddress 用户收货地址
    * @return R
    */
    @PutMapping
    @SaCheckPermission("mall:useraddress:edit")
    public R<Void> updateById(@RequestBody UserAddress userAddress){
        return toAjax(userAddressService.updateById(userAddress));
    }

    /**
    * 通过id删除用户收货地址
    * @param id
    * @return R
    */
    @DeleteMapping("/{id}")
    @SaCheckPermission("mall:useraddress:del")
    public R<Void> removeById(@PathVariable String id){
        return toAjax(userAddressService.removeById(id));
    }

}
